/*
 *  Brno University of Technology
 *
 *  Copyright (C) 2009-2010 by Brno University of Technology and the contributors
 *
 *  Complete list of developers available at our web site:
 *
 *       http://spl.utko.feec.vutbr.cz
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this program.  If not, see http://www.gnu.org/licenses/.
 */
package cz.vutbr.feec.utko.ef.evolution;

import org.apache.log4j.Logger;

import cz.vutbr.feec.utko.ef.core.Config;
import cz.vutbr.feec.utko.ef.evolution.stats.DefaultCrossoverAndMutationRateManager;
import cz.vutbr.feec.utko.ef.gp.tree.TreeChromozome;
import cz.vutbr.feec.utko.ef.grammar.Grammar;

/**
 * The Class DefaultEvolutionSpecifierTree defines the evolution for tree based
 * genetic programming.
 */
public class DefaultEvolutionSpecifierTree extends DefaultEvolutionSpecifier {

	/** The grammar. */
	private Grammar grammar;

	/** The log. */
	private static Logger log = Logger
			.getLogger(DefaultEvolutionSpecifierTree.class);

	/**
	 * Instantiates a new default evolution specifier tree.
	 * 
	 * @param cfg the cfg
	 * @param eval the eval
	 * @param grammar the grammar
	 */
	public DefaultEvolutionSpecifierTree(Config cfg, IFitnessEvaluator eval, Grammar grammar) {
		this(cfg, eval, grammar, new DefaultCrossoverAndMutationRateManager(cfg));
	}

	/**
	 * Instantiates a new default evolution specifier tree.
	 * 
	 * @param cfg the cfg
	 * @param eval the eval
	 * @param grammar the grammar
	 * @param cmrm the cmrm
	 */
	public DefaultEvolutionSpecifierTree(Config cfg, IFitnessEvaluator eval,
			Grammar grammar, DefaultCrossoverAndMutationRateManager cmrm) {
		super(cfg, eval, cmrm);
		this.grammar = grammar;
		grammar.compile();
	}

	/* (non-Javadoc)
	 * @see cz.vutbr.feec.utko.ef.evolution.DefaultEvolutionSpecifier#createNewChromozome(cz.vutbr.feec.utko.ef.core.Config)
	 */
	@Override
	public Chromozome createNewChromozome(Config config) {
		// Create individual according to INI configuration
		TreeChromozome newChromozome = new TreeChromozome(grammar, config
				.getMaxTreeHeight());

		// CHANGED - it's not allowed to generate initial population with
		// chromozome = (root and terminal node)
		// because there is no action used
		if (newChromozome.getRoot().getWeight() > 2) {
			log.debug("Inserted new individual " + newChromozome);

			// Immediate fitness computation
			assignFitness(newChromozome);
			if (newChromozome.getCachedFitness() > config.getMinimalFitness()) {
				return newChromozome;
			}
		}
		// failed
		return null;
	}

}
